#! /bin/bash

if [[ $# < 1 ]]
then
	echo "Expected at least one argument"
	exit 1
fi

ZONEDB=$1
ZONEDB_FILE="/usr/local/etc/bind/zones/$ZONEDB"

for ZONE in $@
do
	if [[ $ZONE = $ZONEDB ]]
	then
		continue
	fi
	while [[ ! -f /dsset/dsset-$ZONE ]]
	do
		echo "Waiting for" /dsset/dsset-$ZONE "to exist"
		sleep 1
	done
	while IFS= read -r DSREC; do
        # 跳过空行
        [[ -z "$DSREC" ]] && continue
        
        # 检查记录是否已存在（使用grep -F固定字符串匹配）
        if ! grep -Fq -- "$DSREC" "$ZONEDB_FILE"; then
            # 追加缺失的DS记录
            echo "$DSREC" >> "$ZONEDB_FILE"
            echo "Added DS record: $DSREC"
        fi
    done < "/dsset/dsset-$ZONE"
done
